package com.guoyansoft.controller;

import com.guoyansoft.config.R;
import com.guoyansoft.model.pojo.Customer;
import com.guoyansoft.model.pojo.PendingOrders;
import com.guoyansoft.model.pojo.Yardman;
import com.guoyansoft.model.service.PendingOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 刘耀诚
 * 待处理订单业务控制器
 */
@RestController
@RequestMapping("/pendingOrders")
public class PendingOrdersController {
    private String no="JinXT000";
    private Integer count=5;
    @Autowired
    private PendingOrdersService pendingOrdersService;

    /**
     * 新增待处理订单------用户下单
     * @param pendingOrders 待处理订单实体
     * @return 响应结果
     */
    @RequestMapping("/save")
    public R save(PendingOrders pendingOrders,Integer customerId,String customerName,String customerPhone){
        Customer customer = new Customer();
        customer.setId(customerId);
        customer.setName(customerName);
        customer.setPhone(customerPhone);
        pendingOrders.setCustomer(customer);
        String id=no+count;
        pendingOrders.setId(id);
        pendingOrders.setState("待处理");
        count++;
        return pendingOrdersService.save(pendingOrders);
    }

    /**
     * 修改待处理订单------用户修改
     * @param pendingOrders 待处理订单实体
     * @return 响应结果
     */
    @RequestMapping("/modify")
    public R modify(PendingOrders pendingOrders,Integer customerId,String customerName,String customerPhone){
        Customer customer=new Customer();
        customer.setId(customerId);
        customer.setName(customerName);
        customer.setPhone(customerPhone);
        Date date =new Date();
        pendingOrders.setOrderTime(date);
        pendingOrders.setCustomer(customer);
        return pendingOrdersService.modify(pendingOrders);
    }

    /**
     * 修改待处理订单------用户删除
     * @param id 待处理订单实体
     * @return 响应结果
     */
    @RequestMapping("/drop")
    public R drop(String[] id){
        return pendingOrdersService.drop(id);
    }

    /**
     * 多条件分页展示
     * @param customPhone 客户电话
     * @param state 订单状态
     * @param curPage 当前页
     * @param pageSize 每页记录数
     * @return 响应数据
     */
    @RequestMapping("/queryByPage")
    public R queryByPage(String customPhone, String state,Integer curPage,Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        map.put("customPhone",customPhone);
        map.put("state",state);
        map.put("curPage",curPage);
        map.put("pageSize",pageSize);
        return pendingOrdersService.queryByPage(map);
    }
    @RequestMapping("queryByYardmanPage")
    public R queryByYardmanPage(Integer yardmanId,Integer curPage,Integer pageSize){

        Map<String,Object> map=new HashMap<>();
        map.put("yardmanId",yardmanId);
        map.put("startIndex",(curPage-1)*pageSize);
        map.put("pageSize",pageSize);

        return pendingOrdersService.queryByYardmanPage(map);
    }

    @RequestMapping("queryByCourierPage")
    public R queryByCourierPage(String province,String city,String district,Integer curPage,Integer pageSize){

        Map<String,Object> map=new HashMap<>();
        String str=province+","+city+","+district;
        map.put("str",str);
        map.put("startIndex",(curPage-1)*pageSize);
        map.put("pageSize",pageSize);
        return pendingOrdersService.queryByCourierPage(map);
    }
    @RequestMapping("modifyState")
    public R modifyState(String id,String state){
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("state",state);
        return pendingOrdersService.modifyState(map);
    }

    @RequestMapping("queryByCourierPendingOrders")
    public R queryByCourierPendingOrders(Integer courierId,Integer curPage,Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        map.put("courierId",courierId);
        map.put("startIndex",(curPage-1)*pageSize);
        map.put("pageSize",pageSize);
        return pendingOrdersService.queryByCourierPendingOrders(map);
       }
    @RequestMapping("queryByCustomer")
    public R queryByCustomer(Customer customer){
        return pendingOrdersService.queryByCustomer(customer);
    }
}
